package cn.bdqn.anno;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

public class SpringFactory {
    //用工厂来存放创建的对象
  static   Map<String,Object> map=new HashMap<>();
  static {
      try {
          Class clz= Class.forName("cn.bdqn.anno.dao.UserDao");
          if (clz.isAnnotationPresent(Component.class)){
              //创建对象
              Object o = clz.newInstance();
              //获得注解对象
              Component annotation = (Component) clz.getAnnotation(Component.class);
              //将注解中value属性值作为key，对象作为值存入map中
              map.put(annotation.value(),o);
          }
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      } catch (InstantiationException e) {
          e.printStackTrace();
      }
  }

  public Object getBean(String id){
      return map.get(id);
  }
}
